/*     */ package com.zimbra.cs.datasource.imap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Folder.FolderOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailclient.imap.Flags;
/*     */ import com.zimbra.cs.mailclient.imap.ListData;
/*     */ import java.util.HashSet;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class LocalFolder
/*     */ {
/*     */   private final Mailbox mbox;
/*     */   private final String path;
/*     */   private Folder folder;
/*  38 */   private static final Log LOG = ZimbraLog.datasource;
/*     */   
/*     */   public static LocalFolder fromId(Mailbox mbox, int id) throws ServiceException {
/*     */     try {
/*  42 */       return new LocalFolder(mbox, mbox.getFolderById(null, id));
/*     */     } catch (MailServiceException.NoSuchItemException e) {}
/*  44 */     return null;
/*     */   }
/*     */   
/*     */   public static LocalFolder fromPath(Mailbox mbox, String path) throws ServiceException
/*     */   {
/*     */     try {
/*  50 */       return new LocalFolder(mbox, mbox.getFolderByPath(null, path));
/*     */     } catch (MailServiceException.NoSuchItemException e) {}
/*  52 */     return null;
/*     */   }
/*     */   
/*     */   LocalFolder(Mailbox mbox, String path)
/*     */   {
/*  57 */     this.mbox = mbox;
/*  58 */     this.path = path;
/*     */   }
/*     */   
/*     */   LocalFolder(Mailbox mbox, Folder folder) {
/*  62 */     this.mbox = mbox;
/*  63 */     this.path = folder.getPath();
/*  64 */     this.folder = folder;
/*     */   }
/*     */   
/*     */   public void delete() throws ServiceException {
/*  68 */     debug("deleting folder", new Object[0]);
/*     */     try {
/*  70 */       getFolder();
/*     */     } catch (MailServiceException.NoSuchItemException e) {
/*  72 */       return;
/*     */     }
/*  74 */     this.mbox.delete(null, this.folder.getId(), this.folder.getType());
/*     */   }
/*     */   
/*     */   public void create() throws ServiceException {
/*  78 */     debug("creating folder", new Object[0]);
/*  79 */     this.folder = this.mbox.createFolder(null, this.path, new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
/*     */   }
/*     */   
/*     */   public void updateFlags(ListData ld) throws ServiceException {
/*  83 */     getFolder();
/*  84 */     if (this.folder.getId() < 256) { return;
/*     */     }
/*  86 */     Flags flags = ld.getFlags();
/*  87 */     boolean noinferiors = (flags.isNoinferiors()) || (ld.getDelimiter() == 0);
/*  88 */     int bits = this.folder.getFlagBitmask();
/*  89 */     if (((bits & Flag.BITMASK_NO_INFERIORS) != 0) != noinferiors) {
/*  90 */       debug("Setting NO_INFERIORS flag to " + noinferiors, new Object[0]);
/*  91 */       alterTag(Flag.FlagInfo.NO_INFERIORS, noinferiors);
/*     */     }
/*  93 */     boolean sync = !flags.isNoselect();
/*  94 */     if (((bits & Flag.BITMASK_SYNCFOLDER) != 0) != sync) {
/*  95 */       debug("Setting sync flag to " + sync, new Object[0]);
/*  96 */       alterTag(Flag.FlagInfo.SYNCFOLDER, sync);
/*  97 */       alterTag(Flag.FlagInfo.SYNC, sync);
/*     */     }
/*  99 */     if (this.folder.getDefaultView() != MailItem.Type.MESSAGE) {
/* 100 */       debug("Setting default view to TYPE_MESSAGE", new Object[0]);
/* 101 */       this.mbox.setFolderDefaultView(null, this.folder.getId(), MailItem.Type.MESSAGE);
/*     */     }
/*     */   }
/*     */   
/*     */   public void alterTag(Flag.FlagInfo finfo, boolean value) throws ServiceException {
/* 106 */     this.mbox.alterTag(null, getFolder().getId(), MailItem.Type.FOLDER, finfo, value, null);
/*     */   }
/*     */   
/*     */   public void setMessageFlags(int id, int flagMask) throws ServiceException {
/* 110 */     this.mbox.setTags(null, id, MailItem.Type.MESSAGE, flagMask, MailItem.TAG_UNCHANGED);
/*     */   }
/*     */   
/*     */   public boolean exists() throws ServiceException {
/*     */     try {
/* 115 */       getFolder();
/*     */     } catch (MailServiceException.NoSuchItemException e) {
/* 117 */       return false;
/*     */     }
/* 119 */     return true;
/*     */   }
/*     */   
/*     */   public Message getMessage(int id) throws ServiceException {
/*     */     try {
/* 124 */       Message msg = this.mbox.getMessageById(null, id);
/* 125 */       if (msg.getFolderId() == getFolder().getId()) {
/* 126 */         return msg;
/*     */       }
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException e) {}
/* 130 */     return null;
/*     */   }
/*     */   
/*     */   public void deleteMessage(int id) throws ServiceException {
/* 134 */     debug("deleting message with id %d", new Object[] { Integer.valueOf(id) });
/*     */     try {
/* 136 */       this.mbox.delete(null, id, MailItem.Type.UNKNOWN);
/*     */     } catch (MailServiceException.NoSuchItemException e) {
/* 138 */       debug("message with id %d not found", new Object[] { Integer.valueOf(id) });
/*     */     }
/*     */   }
/*     */   
/*     */   public void emptyFolder() throws ServiceException {
/* 143 */     this.mbox.emptyFolder(null, getId(), false);
/*     */   }
/*     */   
/*     */   public Set<Integer> getMessageIds() throws ServiceException {
/* 147 */     return new HashSet(this.mbox.listItemIds(null, MailItem.Type.MESSAGE, this.folder.getId()));
/*     */   }
/*     */   
/*     */   public Folder getFolder() throws ServiceException {
/* 151 */     if (this.folder == null) {
/* 152 */       this.folder = this.mbox.getFolderByPath(null, this.path);
/*     */     }
/* 154 */     return this.folder;
/*     */   }
/*     */   
/*     */   public int getId() throws ServiceException {
/* 158 */     return getFolder().getId();
/*     */   }
/*     */   
/*     */   public boolean isInbox() throws ServiceException {
/* 162 */     return getFolder().getId() == 2;
/*     */   }
/*     */   
/*     */   public String getPath() {
/* 166 */     return this.folder != null ? this.folder.getPath() : this.path;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean isKnown()
/*     */   {
/* 176 */     switch (this.folder.getId()) {
/*     */     case 2: 
/*     */     case 3: 
/*     */     case 4: 
/*     */     case 5: 
/*     */     case 6: 
/* 182 */       return true;
/*     */     }
/* 184 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isSystem()
/*     */   {
/* 189 */     return this.folder.getId() < 256;
/*     */   }
/*     */   
/*     */   public void debug(String fmt, Object... args) {
/* 193 */     if (LOG.isDebugEnabled()) {
/* 194 */       LOG.debug(errmsg(String.format(fmt, args)));
/*     */     }
/*     */   }
/*     */   
/*     */   public void info(String fmt, Object... args) {
/* 199 */     LOG.info(errmsg(String.format(fmt, args)));
/*     */   }
/*     */   
/*     */   public void warn(String msg, Throwable e) {
/* 203 */     LOG.error(errmsg(msg), e);
/*     */   }
/*     */   
/*     */   public void error(String msg, Throwable e) {
/* 207 */     LOG.error(errmsg(msg), e);
/*     */   }
/*     */   
/*     */   private String errmsg(String s) {
/* 211 */     return String.format("Local folder '%s': %s", new Object[] { getPath(), s });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/imap/LocalFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */